package androcr.client.mobile.core;

import java.util.UUID;

import androcr.client.mobile.display.servicechooser.ServiceChooserActivity;
import androcr.client.mobile.display.texttospeech.TextToSpeechActivity;
import androcr.client.mobile.display.translator.TranslatorActivity;
import androcr.client.mobile.services.fileuploader.IUploadCompleteListener;
import androcr.client.mobile.services.fileuploader.Uploader;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.util.Log;

public class Controller {
	private static final String TAG = "Controller";
	
	private static final Controller INSTANCE = new Controller();

	private Uploader uploader = new Uploader();

	private byte[] data; // TODO from config ----- data package
	private String actualRecognizedText = ""; // TODO from config ----- data
												// package

	// Private constructor prevents instantiation from other classes
	private Controller() {
	}

	public static Controller getInstance() {
		return INSTANCE;
	}

	public void uploadFile(IUploadCompleteListener ucl, Context context,
			byte[] data) {
		uploader.addUploadCompleteListener(ucl);
		uploader.uploadImage(context, data);
	}

	public String getActualRecognizedText() {
		return actualRecognizedText;
	}

	public void setActualRecognizedText(String actualRecognizedText) {
		actualRecognizedText = actualRecognizedText.replace('-', ' ');
		actualRecognizedText = actualRecognizedText.replace('_', '\n');
		this.actualRecognizedText = actualRecognizedText;
	}

	public void startPictureProcessor(Context context, byte[] data) {
		this.data = data;

		Intent intentServiceChooser = new Intent((Context) context,
				ServiceChooserActivity.class);
		context.startActivity(intentServiceChooser);
	}

	public void startTextToSpeechService(Context context) {
		Intent ttsIntent = new Intent(context, TextToSpeechActivity.class);
		context.startActivity(ttsIntent);
	}

	public byte[] getPictureData() {
		return data;
	}

	public String getDeviceID(Context context) {
		ContextWrapper cw = new ContextWrapper(context);
		final TelephonyManager tm = (TelephonyManager) cw.getBaseContext()
				.getSystemService(Context.TELEPHONY_SERVICE);
		
		final String tmDevice, tmSerial;
		tmDevice = "" + tm.getDeviceId();
		tmSerial = "" + tm.getSimSerialNumber();
		
		Log.i(TAG, "device tm " + tmDevice);
		Log.i(TAG, "sim serial tm " + tmSerial);

		UUID deviceUuid = new UUID((long) tmDevice.hashCode(), tmSerial.hashCode());
		return deviceUuid.toString();
	}

	public void startTranslator(Context context) {
		Intent translatorIntent = new Intent(context, TranslatorActivity.class);
		context.startActivity(translatorIntent);
	}

	public void startWebSearchService(Context context, String text) {
		String url = "http://www.google.ro/search?ie=UTF-8&q=";

		Intent i = new Intent(Intent.ACTION_VIEW);

		i.setData(Uri.parse(url+text));

		context.startActivity(i);
	}

}